﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Robotics.Race
{
	public partial class CarConfigurationPanel : UserControl
	{
		private Car car = null;
		public Car Car { get { return car; } }
		public NxtConfiguration Nxt { get { return Car.Nxt; } }

		public String CarName
        {
            get { return tbName.Text; }
			set { tbName.Text = value; }
        }

		public CarConfigurationPanel()
		{
			InitializeComponent();

			cbMotorLeft.Items.Add( Bram.NxtSharp.NxtMotorPort.None );
			cbMotorLeft.Items.Add( Bram.NxtSharp.NxtMotorPort.PortA );
			cbMotorLeft.Items.Add( Bram.NxtSharp.NxtMotorPort.PortB );
			cbMotorLeft.Items.Add( Bram.NxtSharp.NxtMotorPort.PortC );

			cbMotorRight.Items.Add( Bram.NxtSharp.NxtMotorPort.None );
			cbMotorRight.Items.Add( Bram.NxtSharp.NxtMotorPort.PortA );
			cbMotorRight.Items.Add( Bram.NxtSharp.NxtMotorPort.PortB );
			cbMotorRight.Items.Add( Bram.NxtSharp.NxtMotorPort.PortC );

			cbLightSensorLeft.Items.Add( Bram.NxtSharp.NxtSensorPort.None );
			cbLightSensorLeft.Items.Add( Bram.NxtSharp.NxtSensorPort.Port1 );
			cbLightSensorLeft.Items.Add( Bram.NxtSharp.NxtSensorPort.Port2 );
			cbLightSensorLeft.Items.Add( Bram.NxtSharp.NxtSensorPort.Port3 );
			cbLightSensorLeft.Items.Add( Bram.NxtSharp.NxtSensorPort.Port4 );

			cbLightSensorRight.Items.Add( Bram.NxtSharp.NxtSensorPort.None );
			cbLightSensorRight.Items.Add( Bram.NxtSharp.NxtSensorPort.Port1 );
			cbLightSensorRight.Items.Add( Bram.NxtSharp.NxtSensorPort.Port2 );
			cbLightSensorRight.Items.Add( Bram.NxtSharp.NxtSensorPort.Port3 );
			cbLightSensorRight.Items.Add( Bram.NxtSharp.NxtSensorPort.Port4 );

			cbPressureSensor.Items.Add( Bram.NxtSharp.NxtSensorPort.None );
			cbPressureSensor.Items.Add( Bram.NxtSharp.NxtSensorPort.Port1 );
			cbPressureSensor.Items.Add( Bram.NxtSharp.NxtSensorPort.Port2 );
			cbPressureSensor.Items.Add( Bram.NxtSharp.NxtSensorPort.Port3 );
			cbPressureSensor.Items.Add( Bram.NxtSharp.NxtSensorPort.Port4 );

            cbSonar.Items.Add( Bram.NxtSharp.NxtSensorPort.None );
            cbSonar.Items.Add( Bram.NxtSharp.NxtSensorPort.Port1 );
            cbSonar.Items.Add( Bram.NxtSharp.NxtSensorPort.Port2 );
            cbSonar.Items.Add( Bram.NxtSharp.NxtSensorPort.Port3 );
            cbSonar.Items.Add( Bram.NxtSharp.NxtSensorPort.Port4 );
		}

		public void LoadSettings( Car car )
		{
			if ( car == null )
				throw new NullReferenceException();

			this.car = car;

			CarName = car.Name;

			updComPort.Value = decimal.Parse( car.Nxt.Brick.COMPortName.Substring( 3 ) );

			cbMotorLeft.SelectedItem = car.Nxt.MotorLeft.Port;
			cbMotorRight.SelectedItem = car.Nxt.MotorRight.Port;
			cbLightSensorLeft.SelectedItem = car.Nxt.LightSensorLeft.Port;
			cbLightSensorRight.SelectedItem = car.Nxt.LightSensorRight.Port;
			cbSonar.SelectedItem = car.Nxt.Sonar.Port;
			cbPressureSensor.SelectedItem = car.Nxt.PressureSensor.Port;
		}

		public void SaveSettings()
		{
			Nxt.Brick.AttachMotor( (Bram.NxtSharp.NxtMotorPort)cbMotorLeft.SelectedItem, Nxt.MotorLeft );
			Nxt.Brick.AttachMotor( (Bram.NxtSharp.NxtMotorPort)cbMotorRight.SelectedItem, Nxt.MotorRight );

			Nxt.MotorLeft.Port = (Bram.NxtSharp.NxtMotorPort)cbMotorLeft.SelectedItem;
			Nxt.MotorRight.Port = (Bram.NxtSharp.NxtMotorPort)cbMotorRight.SelectedItem;

            Nxt.LightSensorLeft.Port = (Bram.NxtSharp.NxtSensorPort)cbLightSensorLeft.SelectedItem;
            Nxt.LightSensorRight.Port = (Bram.NxtSharp.NxtSensorPort)cbLightSensorRight.SelectedItem;
            Nxt.PressureSensor.Port = (Bram.NxtSharp.NxtSensorPort)cbPressureSensor.SelectedItem;
            Nxt.Sonar.Port = (Bram.NxtSharp.NxtSensorPort)cbSonar.SelectedItem;

			Nxt.Brick.AttachSensor( (Bram.NxtSharp.NxtSensorPort)cbLightSensorLeft.SelectedItem, Nxt.LightSensorLeft );
			Nxt.Brick.AttachSensor( (Bram.NxtSharp.NxtSensorPort)cbLightSensorRight.SelectedItem, Nxt.LightSensorRight );
			Nxt.Brick.AttachSensor( (Bram.NxtSharp.NxtSensorPort)cbPressureSensor.SelectedItem, Nxt.PressureSensor );
			Nxt.Brick.AttachSensor( (Bram.NxtSharp.NxtSensorPort)cbSonar.SelectedItem, Nxt.Sonar );

			Nxt.Brick.COMPortName = "COM" + updComPort.Value;

			Car.Name = CarName;
		}

        public void Connect()
        {
            try
            {
                SaveSettings();

                lbConnectionState.Text = "Attempt...";
                lbConnectionState.ForeColor = Color.Black;

                Refresh();

                if ( !Nxt.Brick.IsConnected )
                    Nxt.Brick.Connect();

                lbConnectionState.Text = "Connected";
                lbConnectionState.ForeColor = Color.Green;
            }
            catch (Exception)
            {
                lbConnectionState.Text = "Not connected";
                lbConnectionState.ForeColor = Color.Red;
            }
        }

		private void btCheck_Click( object sender, EventArgs e )
		{
            Connect();
		}
	}
}
